﻿using System;
using System.IO;
using System.Xml.Linq;

namespace YArchitech.Temp
{
	public class HYPipeSystemOption : HYSystemOption
	{
		public bool ParameterByTemperature { get; set; }

		public int SupplyTemperature { get; set; }

		public int ReturnTemperature { get; set; }

		public HYPipeSystemOption.CalMethod Method { get; set; }

		public Range DNRange
		{
			get
			{
				return this.m_DNRange;
			}
			set
			{
				this.m_DNRange = value;
			}
		}

		public HYPipeSystemOption()
		{
			base.RoughnessFix = 1.0;
			base.FrictionFix = 1.0;
			base.ShockFix = 1.0;
			base.Roughness = 0.00015;
			base.Density = 1000.0;
			base.Viscosity = 4.79E-07;
			base.TerminalLoss = 10000.0;
			this.ParameterByTemperature = false;
			this.SupplyTemperature = 12;
			this.ReturnTemperature = 7;
			this.Method = HYPipeSystemOption.CalMethod.ByRm;
			this.m_DNRange = new Range(0.0, 0.0);
		}

		public override void WriteXml(XElement xe)
		{
			base.WriteXml(xe);
			xe.SetAttributeValue("ParameterByTemperature", this.ParameterByTemperature);
			xe.SetAttributeValue("SupplyTemperature", this.SupplyTemperature);
			xe.SetAttributeValue("ReturnTemperature", this.ReturnTemperature);
			xe.SetAttributeValue("Method", (int)this.Method);
			XElement xelement = xe.Element("DNRange");
			if (xelement == null)
			{
				xelement = new XElement("DNRange");
				xe.Add(xelement);
			}
			this.m_DNRange.WriteXml(xelement);
		}

		public override void ReadXml(XElement xe)
		{
			base.ReadXml(xe);
			this.ParameterByTemperature = xe.GetBoolAttribute("ParameterByTemperature", false);
			this.SupplyTemperature = xe.GetIntAttribute("SupplyTemperature", 12);
			this.ReturnTemperature = xe.GetIntAttribute("ReturnTemperature", 7);
			int intAttribute = xe.GetIntAttribute("Method", 1);
			this.Method = (HYPipeSystemOption.CalMethod)intAttribute;
			XElement xelement = xe.Element("DNRange");
			if (xelement != null)
			{
				this.m_DNRange.ReadXml(xelement);
			}
		}

		public virtual void Save(string filename)
		{
			XElement xelement;
			if (File.Exists(filename))
			{
				xelement = XElement.Load(filename);
			}
			else
			{
				Directory.CreateDirectory(Path.GetDirectoryName(filename));
				xelement = new XElement("HYUserConfig");
			}
			if (xelement == null)
			{
				return;
			}
			XElement xe = xelement.ChildElement("PipeSystemOption");
			this.WriteXml(xe);
			xelement.Save(filename);
		}

		public virtual void Load(string filename)
		{
			if (!File.Exists(filename))
			{
				return;
			}
			try
			{
				XElement xelement = XElement.Load(filename);
				if (xelement != null)
				{
					XElement xelement2 = xelement.Element("PipeSystemOption");
					if (xelement2 != null)
					{
						this.ReadXml(xelement2);
					}
				}
			}
			catch (Exception ex)
			{
				string message = ex.Message;
			}
		}

		private Range m_DNRange;

		public enum CalMethod
		{
			ByVelocity,
			ByRm,
			ByLoad
		}
	}
}
